package com.jf.blogbackened.controller;

import com.alibaba.fastjson.JSON;
import com.jf.blogbackened.bean.Article;
import com.jf.blogbackened.bean.Result;
import com.jf.blogbackened.service.ArticleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author David
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @GetMapping("/getAllArticles/{currentPage}/{pageSize}")
    public String getAllArticles(@PathVariable Integer currentPage, @PathVariable Integer pageSize ){
        Result res =articleService.getAllArticles(new Article(),currentPage,pageSize);
        return JSON.toJSONString(res);
    }
    @GetMapping("/getPageData")
    public String getPageData(Article article, Integer currentPage, Integer pageSize ){
        Result res =articleService.getAllArticles(article,currentPage,pageSize);
        return JSON.toJSONString(res);
    }
    @PostMapping("/addArticle")
    public String addArticle(@RequestBody Article article){
        Result res = articleService.addOrUpdateArticle(article);
        return JSON.toJSONString(res);
    }

    @GetMapping("/getArticlesById/{id}")
    public String getArticlesById(@PathVariable Integer id ){
        Result res =articleService.getArticlesById(id);
        return JSON.toJSONString(res);
    }

    @GetMapping("/delArticleById/{id}")
    public String delArticleById(@PathVariable Integer id ){
        Result res =articleService.delArticleById(id);
        return JSON.toJSONString(res);
    }
}
